<?php

namespace App\Admin\Controllers;

use App\Models\Coupon;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CouponController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '卡券';

    private function __($key)
    {
        return [
            'Id' => 'ID',
            'UserName' => '会员',
            'Amount' => '金额',
            'Condition amount' => '使用条件',
            'Expired time' => '到期时间',
            'Status' => '状态',
            'Created at' => '创建时间',
            'Updated at' => '修改时间',
        ][$key] ?? $key;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Coupon());

        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableColumnSelector();
        $grid->disableRowSelector();

        $grid->column('id', $this->__('Id'));

        $grid->column('user_id', $this->__('UserName'))->display(function($userId) {
            return User::where('id', $userId)->value('nickname') ?: '--';
        });

        $grid->column('amount', $this->__('Amount'))->filter('range')->sortable();
        $grid->column('condition_amount', $this->__('Condition amount'))->filter('range')->sortable();
        $grid->column('status', $this->__('Status'))->using(Coupon::STATUS)->filter(Coupon::STATUS);
        $grid->column('expired_time', $this->__('Expired time'))->display(function($expired_time) {
            return !empty($expired_time) ? date('Y-m-d', $expired_time) : '--';
        })->sortable();
        $grid->column('created_at', $this->__('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        $grid->column('updated_at', $this->__('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Coupon::findOrFail($id));

        $show->field('id', $this->__('Id'));
        $show->field('user_id',  $this->__('UserName'))->as(function($userId) {
            return User::where('id', $userId)->value('nickname') ?: '--';
        });

        $show->field('amount', $this->__('Amount'));
        $show->field('condition_amount', $this->__('Condition amount'));

        $show->field('status', $this->__('Status'))->using(Coupon::STATUS);

        $show->field('expired_time', $this->__('Expired time'))->as(function($expired_time) {
            return !empty($expired_time) ? date('Y-m-d', $expired_time) : '--';
        });
        $show->field('created_at', $this->__('Created at'))->as(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        $show->field('updated_at', $this->__('Updated at'))->as(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Coupon());
        $model = $form->model();

        $form->hidden('user_id')->default($model->user_id ?: 0);

        $form->currency('amount', $this->__('Amount'))->symbol('￥')->default(0.00)->rules('required');
        $form->currency('condition_amount', $this->__('Condition amount'))->symbol('￥')->default(0.00);
        $form->date('expired_time', $this->__('Expired time'))->format('YYYY-MM-DD')->rules('required')->default($model->expired_time ? date('Y-m-d', $model->expired_time) : null);
        $form->radio('status', $this->__('Status'))->options(Coupon::STATUS)->default($model->status);

        $form->select('user_id', $this->__('会员'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/api/users');

        $form->saving(function (Form $form) {

            $form->user_id = $form->user_id ?: 0;
            $form->expired_time = $form->expired_time ? strtotime($form->expired_time) : null;
        });

        return $form;
    }
}
